<?php

namespace App\Logic;

use App\common;
use App\Models\House;
use Illuminate\Support\Facades\Redis;

class SunHouseLogic
{
    /*
     *  展示找房数据
     */
    public static function sunShow()
    {
        try {

            return House::houseAll();
        } catch (\Exception $exception) {
            return common::fail($exception->getMessage());
        }
    }

    /**
     * 排行榜
     */
    public static function nameAndRent()
    {
        try {
            Redis::select(1); //指定使用1号数据库
            $houseData = House::houseAll();// 房屋数据
            Redis::zAdd('house',$houseData['name'], $houseData['rent'], json_encode($houseData['location']));// 添加房屋信息存到Redis中
            $rankLimit = 10; //前10名排行
            $houses = Redis::zRevRange('houses',0,$rankLimit-1,true);
            $rank = 1;
            $result=[];
            foreach ($houses as $houseRent => $houseData) {
                $house = json_decode($houseData, true);
//                echo "第 $rank 名：{$house['name']}，价格：{$house['price']}\n";
                $rank++;
            }
            return $result;
        } catch (\Exception $exception) {
            return common::fail($exception->getMessage());
        }
    }
}
